﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using CasaShowDados;
using System.Configuration;
using System.Data.SqlClient;

namespace CasaShow {
  public partial class CadastroShow : System.Web.UI.Page {
    protected void Page_Load(object sender, EventArgs e) {
      if (!Page.IsPostBack) {
        PopulaArtistaGV();
        PopulaShowGV();
      }
    }

    #region DetailsView Show

    protected void ShowDV_DataBound(object sender, EventArgs e) {
      int id = 0;
      if (ShowDV.DataKey.Value != null)
        id = Convert.ToInt32(ShowDV.DataKey[0].ToString());

      DataTable u = new DataTable();
      DataTable a = new DataTable();
      AcessoBanco.StringConexao = ConfigurationManager.ConnectionStrings["Show"].ConnectionString;
      u = (AcessoBanco.ConsultarShow(id)).Tables["Table"];
      a = (AcessoBanco.ConsultarArtista()).Tables["Table"];

      ((DropDownList)ShowDV.FindControl("NomeArtistaDdl")).DataSource = a;
      ((DropDownList)ShowDV.FindControl("NomeArtistaDdl")).DataTextField = "NM_ARTISTA";
      ((DropDownList)ShowDV.FindControl("NomeArtistaDdl")).DataValueField = "ID_ARTISTA";
      ((DropDownList)ShowDV.FindControl("NomeArtistaDdl")).DataBind();
      if (u != null && ShowDV.DataKey.Value != null) {
        ((DropDownList)ShowDV.FindControl("NomeArtistaDdl")).SelectedValue = Convert.ToString(u.Rows[0]["ID_ARTISTA"]);
      }
    }

    #endregion

    #region Gridview

    #region gridView Artista

    protected void ArtistaGV_RowDataBound(object sender, GridViewRowEventArgs e) {
    }

    protected void ArtistaGV_PageIndexChanging(object sender, GridViewPageEventArgs e) {
      PopulaArtistaGV();
      ArtistaGV.PageIndex = e.NewPageIndex;
      ArtistaGV.DataBind();
    }

    protected void ArtistaGV_PreRender(object sender, EventArgs e) {
      if (ArtistaGV.BottomPagerRow != null) {
        ((Label)ArtistaGV.BottomPagerRow.FindControl("PaginaAtualLbl")).Text = (ArtistaGV.PageIndex + 1).ToString() + " de " + ArtistaGV.PageCount.ToString() +
          " (Total de Registros: " + ViewState["totalPeriodos"] + ")";
        ((ImageButton)ArtistaGV.BottomPagerRow.FindControl("FirstBtn")).Visible =
        ((ImageButton)ArtistaGV.BottomPagerRow.FindControl("PreviousBtn")).Visible = ArtistaGV.PageIndex != 0;
        ((ImageButton)ArtistaGV.BottomPagerRow.FindControl("NextBtn")).Visible =
        ((ImageButton)ArtistaGV.BottomPagerRow.FindControl("LastBtn")).Visible = ArtistaGV.PageCount != (ArtistaGV.PageIndex + 1);
      }
    }

    protected void ArtistaGV_RowDeleting(object sender, GridViewDeleteEventArgs e) {
      int id = Convert.ToInt32(ArtistaGV.DataKeys[e.RowIndex].Value.ToString());

      List<Artista> artista = new List<Artista>();
      Artista x = new Artista();
      x.CodigoArtista = id;
      x.Acao = Enumeradores.EnumAcaoXML.Excluir;
      artista.Add(x);

      AcessoBanco.StringConexao = (ConfigurationManager.ConnectionStrings["Show"].ConnectionString);
      AcessoBanco.CadastrarArtista(artista);

      PopulaArtistaGV();
    }

    protected void ArtistaGV_RowEditing(object sender, GridViewEditEventArgs e) {
      int id = Convert.ToInt32(ArtistaGV.DataKeys[e.NewEditIndex].Value.ToString());
      IdArtistaHF.Value = id.ToString();
      PopulaArtistaDV(id);
    }

    #endregion

    #region gridView Show

    protected void ShowGV_RowDataBound(object sender, GridViewRowEventArgs e) {

    }

    protected void ShowGV_PageIndexChanging(object sender, GridViewPageEventArgs e) {
      PopulaShowGV();
      ShowGV.PageIndex = e.NewPageIndex;
      ShowGV.DataBind();
    }

    protected void ShowGV_RowDeleting(object sender, GridViewDeleteEventArgs e) {
      int id = Convert.ToInt32(ShowGV.DataKeys[e.RowIndex].Value.ToString());

      List<Show> show = new List<Show>();
      Show x = new Show();
      x.CodigoShow = id;
      x.DataShow = DateTime.Now;
      x.Acao = Enumeradores.EnumAcaoXML.Excluir;
      show.Add(x);

      AcessoBanco.StringConexao = (ConfigurationManager.ConnectionStrings["Show"].ConnectionString);
      AcessoBanco.CadastrarShow(show);

      PopulaShowGV();
    }

    protected void ShowGV_PreRender(object sender, EventArgs e) {
      if (ShowGV.BottomPagerRow != null) {
        ((Label)ShowGV.BottomPagerRow.FindControl("PaginaAtualLbl")).Text = (ShowGV.PageIndex + 1).ToString() + " de " + ShowGV.PageCount.ToString() +
          " (Total de Registros: " + ViewState["totalPeriodos"] + ")";
        ((ImageButton)ShowGV.BottomPagerRow.FindControl("FirstBtn")).Visible =
        ((ImageButton)ShowGV.BottomPagerRow.FindControl("PreviousBtn")).Visible = ShowGV.PageIndex != 0;
        ((ImageButton)ShowGV.BottomPagerRow.FindControl("NextBtn")).Visible =
        ((ImageButton)ShowGV.BottomPagerRow.FindControl("LastBtn")).Visible = ShowGV.PageCount != (ShowGV.PageIndex + 1);
      }
    }

    protected void ShowGV_RowEditing(object sender, GridViewEditEventArgs e) {
      int id = Convert.ToInt32(ShowGV.DataKeys[e.NewEditIndex].Value.ToString());
      IdShowHF.Value = id.ToString();
      PopulaShowDV(id);
    }

    #endregion

    #endregion

    #region Métodos

    #region Métodos Artista

    private List<Artista> ConverterTabelaArtistaEmLista(DataTable dataTable) {
      List<Artista> resultado = null;
      Artista u = new Artista();
      if (dataTable.Rows.Count > 0) {
        resultado = new List<Artista>();
        for (int i = 0; i < dataTable.Rows.Count; i++) {
          resultado.Add(u);
          resultado[i].Acao = Enumeradores.EnumAcaoXML.Alterar;
          resultado[i].ArtistaDisponivel = true;
          resultado[i].CodigoArtista = Convert.ToInt32(dataTable.Rows[i]["ID_ARTISTA"]);
          resultado[i].NomeArtista = Convert.ToString(dataTable.Rows[i]["NM_ARTISTA"]);
        }
      }

      return resultado;
    }

    private List<Artista> ObterDadosAtistaDetailsView() {
      List<Artista> resultado = new List<Artista>();
      Artista temp = new Artista();

      temp.Acao = Enumeradores.EnumAcaoXML.Incluir;
      temp.ArtistaDisponivel = true;
      temp.NomeArtista = ((TextBox)ArtistaDV.FindControl("NomeArtistaTxt")).Text;

      resultado.Add(temp);

      return resultado;
    }

    private void PopulaArtistaGV() {
      DataSet artista = new DataSet();
      AcessoBanco.StringConexao = ConfigurationManager.ConnectionStrings["Show"].ConnectionString;
      artista = AcessoBanco.ConsultarArtista();
      if (artista.Tables["Table"].Rows.Count > 0) {
        ArtistaGV.DataSource = artista.Tables["Table"];
      }
      ArtistaGV.DataBind();
    }

    private void MostraGridViewArtista() {
      if (ArtistaDV.Visible == true) {
        ArtistaDV.Visible = false;
      }
      SalvarBtn.Visible = false;
      NovoBtn.Visible = ArtistaGV.Visible = true;
      PopulaArtistaGV();
    }

    private void PopulaArtistaDV(int id) {
      IdArtistaHF.Value = id.ToString();
      DataSet artista = new DataSet();
      AcessoBanco.StringConexao = ConfigurationManager.ConnectionStrings["Show"].ConnectionString;
      artista = AcessoBanco.ConsultarArtista(id);
      if (artista.Tables["Table"].Rows.Count > 0) {
        ArtistaDV.ChangeMode(DetailsViewMode.Edit);
        MostraArtistaDV();
        ArtistaDV.DataSource = artista.Tables["Table"];
        ArtistaDV.DataBind();
      }
    }

    private void MostraArtistaDV() {
      if (ArtistaDV.Visible == false) {
        ArtistaDV.Visible = true;
      }
      SalvarBtn.Visible = true;
      NovoBtn.Visible = ArtistaGV.Visible = false;
    }

    #endregion

    #region Métodos show

    private void PopulaShowGV() {
      DataSet show = new DataSet();
      AcessoBanco.StringConexao = ConfigurationManager.ConnectionStrings["Show"].ConnectionString;
      show = AcessoBanco.ConsultarShow();
      if (show.Tables["Table"].Rows.Count > 0) {
        ShowGV.DataSource = show.Tables["Table"];
      }
      ShowGV.DataBind();
    }

    private List<Show> ConverterTabelaShowEmLista(DataTable dataTable) {
      List<Show> resultado = null;
      Show u = new Show();
      if (dataTable.Rows.Count > 0) {
        resultado = new List<Show>();
        for (int i = 0; i < dataTable.Rows.Count; i++) {
          resultado.Add(u);
          resultado[i].Acao = Enumeradores.EnumAcaoXML.Alterar;
          resultado[i].CodigoArtista = Convert.ToInt32(dataTable.Rows[i]["ID_ARTISTA"]);
          resultado[i].NomeArtista = (dataTable.Rows[i]["NM_ARTISTA"]).ToString();
          resultado[i].CodigoShow = Convert.ToInt32(dataTable.Rows[i]["ID_SHOW"]);
          resultado[i].DataShow = Convert.ToDateTime(dataTable.Rows[i]["DT_SHOW"]);
          resultado[i].DuracaoShowEmMinutos = Convert.ToInt32(dataTable.Rows[i]["QT_DURACAO"]);
          resultado[i].ShowDisponivel = Convert.ToBoolean(dataTable.Rows[i]["IC_SHOW_DISPONIVEL"]);
          resultado[i].ValorShowEmReais = Convert.ToDouble(dataTable.Rows[i]["VL_SHOW"]);
        }
      }

      return resultado;
    }

    private List<Show> ObterDadosShowDetailsView() {
      List<Show> resultado = new List<Show>();
      Show temp = new Show();

      temp.Acao = Enumeradores.EnumAcaoXML.Incluir;
      temp.CodigoArtista = Convert.ToInt32(((DropDownList)ShowDV.FindControl("NomeArtistaDdl")).SelectedValue);
      temp.DataShow = Convert.ToDateTime(((TextBox)ShowDV.FindControl("DataShowTxt")).Text);
      temp.DuracaoShowEmMinutos = Convert.ToInt32(((TextBox)ShowDV.FindControl("DuracaoTxt")).Text);
      temp.ValorShowEmReais = Convert.ToDouble(((TextBox)ShowDV.FindControl("ValorTxt")).Text);
      temp.ShowDisponivel = true;

      resultado.Add(temp);

      return resultado;
    }

    private void PopulaShowDV(int id) {
      IdShowHF.Value = id.ToString();
      DataSet show = new DataSet();
      AcessoBanco.StringConexao = ConfigurationManager.ConnectionStrings["Show"].ConnectionString;
      show = AcessoBanco.ConsultarShow(id);
      if (show.Tables["Table"].Rows.Count > 0) {
        ShowDV.ChangeMode(DetailsViewMode.Edit);
        MostraShowDV();
        ShowDV.DataSource = show.Tables["Table"];
        ShowDV.DataBind();
      }
    }

    private void MostraShowDV() {
      if (ShowDV.Visible == false) {
        ShowDV.Visible = true;
      }
      SalvarShowBtn.Visible = true;
      NovoShowBtn.Visible = ShowGV.Visible = false;
    }

    private void MostraGridViewShow() {
      if (ShowDV.Visible == true) {
        ShowDV.Visible = false;
      }
      SalvarShowBtn.Visible = false;
      NovoShowBtn.Visible = ShowGV.Visible = true;
      PopulaShowGV();
    }

    #endregion

    #endregion

    #region Botões

    #region Botões Artista

    protected void NovoBtn_Click(object sender, ImageClickEventArgs e) {
      ArtistaDV.ChangeMode(DetailsViewMode.Insert);
      if (ArtistaDV.Visible == false) {
        ArtistaDV.Visible = true;
      }
      SalvarBtn.Visible = true;
      NovoBtn.Visible = ArtistaGV.Visible = false;
    }

    protected void VoltarBtn_Click(object sender, ImageClickEventArgs e) {
      ArtistaDV.ChangeMode(DetailsViewMode.Edit);
      if (ArtistaDV.Visible == true) {
        ArtistaDV.Visible = false;
      }
      SalvarBtn.Visible = false;
      NovoBtn.Visible = ArtistaGV.Visible = true;
    }

    protected void SalvarBtn_Click(object sender, ImageClickEventArgs e) {
      try {
        AcessoBanco.StringConexao = ConfigurationManager.ConnectionStrings["Show"].ConnectionString;
        List<Artista> artistaLista = null;

        if (ArtistaDV.CurrentMode == DetailsViewMode.Edit) {
          DataSet artista = new DataSet();
          int id = Convert.ToInt32(IdArtistaHF.Value);
          artista = AcessoBanco.ConsultarArtista(id);
          artistaLista = ConverterTabelaArtistaEmLista(artista.Tables["Table"]);
        }
        artistaLista = ObterDadosAtistaDetailsView();

        if (ArtistaDV.CurrentMode == DetailsViewMode.Edit) {
          artistaLista[0].Acao = Enumeradores.EnumAcaoXML.Alterar;
          artistaLista[0].CodigoArtista = Convert.ToInt32(IdArtistaHF.Value);
        }
        else if (ArtistaDV.CurrentMode == DetailsViewMode.Insert)
          artistaLista[0].Acao = Enumeradores.EnumAcaoXML.Incluir;

        AcessoBanco.CadastrarArtista(artistaLista);
        MostraGridViewArtista();
      }
      catch (Exception ex) {
        InfoLbl.Text = ex.Message;
        InfoLbl.Visible = true;
      }
    }

    #endregion

    #region Botões Show

    protected void NovoShowBtn_Click(object sender, ImageClickEventArgs e) {
      ShowDV.ChangeMode(DetailsViewMode.Insert);
      if (ShowDV.Visible == false) {
        ShowDV.Visible = true;
      }
      SalvarShowBtn.Visible = true;
      NovoShowBtn.Visible = ShowGV.Visible = false;
    }

    protected void VoltarShowBtn_Click(object sender, ImageClickEventArgs e) {
      ShowDV.ChangeMode(DetailsViewMode.Edit);
      if (ShowDV.Visible == true) {
        ShowDV.Visible = false;
      }
      SalvarShowBtn.Visible = false;
      NovoShowBtn.Visible = ShowGV.Visible = true;
    }

    protected void SalvarShowBtn_Click(object sender, ImageClickEventArgs e) {
      try {
        AcessoBanco.StringConexao = ConfigurationManager.ConnectionStrings["Show"].ConnectionString;
        List<Show> showLista = null;

        if (ShowDV.CurrentMode == DetailsViewMode.Edit) {
          DataSet show = new DataSet();
          int id = Convert.ToInt32(IdShowHF.Value);
          show = AcessoBanco.ConsultarShow(id);
          showLista = ConverterTabelaShowEmLista(show.Tables["Table"]);
        }
        showLista = ObterDadosShowDetailsView();

        if (ShowDV.CurrentMode == DetailsViewMode.Edit) {
          showLista[0].Acao = Enumeradores.EnumAcaoXML.Alterar;
          showLista[0].CodigoShow = Convert.ToInt32(IdShowHF.Value);
        }
        else if (ShowDV.CurrentMode == DetailsViewMode.Insert)
          showLista[0].Acao = Enumeradores.EnumAcaoXML.Incluir;

        AcessoBanco.CadastrarShow(showLista);
        MostraGridViewShow();
      }
      catch (Exception ex) {
        InfoLbl.Text = ex.Message;
        InfoLbl.Visible = true;
      }
    }

    #endregion

    #endregion

  }
}
